
package finalproject.client;

import java.util.List;

import finalproject.shared.Docent;
import finalproject.shared.Persoon;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HTMLTable.Cell;


public class DocentGui {
 
    private static final String LISTING_ROOT_PANEL = "guiListing";
    private static final String FORM_ROOT_PANEL = "guiForm";
    private static final String TOOL_BAR_ROOT_PANEL = "guiToolBar";
    private static final int EDIT_LINK = 3;
    private static final int REMOVE_LINK = 4;

  //widgets
    protected Button addButton;
    protected Button updateButton;
    protected Button addNewButton;
    protected TextBox naamField;
    protected TextBox kamerField;
    protected TextBox emailField;
    protected Grid listGrid;
    protected Grid formGrid;
    
    /* Data model */
    private List<Docent> Docenten;
    private Docent currentDocent;
    private Persoon currentPersoon;
    protected DocentClass DocentService;
        
//add
    public void go() {
        addButton = new Button("Add Docent");
        addNewButton = new Button("Add new Docent");
        updateButton = new Button("Update Docent");
        naamField = new TextBox();
        kamerField = new TextBox();
      emailField = new TextBox();
        listGrid = new Grid(2,6);

        buildForm();
        placeWidgets();
    }
 
  //events
    public void wireEvents() {
    	listGrid.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
                 Cell cellForEvent = listGrid.getCellForEvent(event);
                 gui_eventListGridClicked(cellForEvent);                
            }});
        
    	addButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
            	gui_eventAddButtonClicked();
            }});

    	updateButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
            	gui_eventUpdateButtonClicked();
            }});
        
    	addNewButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
            	gui_eventAddNewButtonClicked();
            }});

    }
    
    
    //opbouw
    private void buildForm() {
        formGrid = new Grid(5,3);
        formGrid.setVisible(false);
        
        formGrid.setWidget(0, 0, new Label("Naam"));
        formGrid.setWidget(0, 1, naamField);

        formGrid.setWidget(1, 0, new Label("Email"));
        formGrid.setWidget(1, 1, emailField);
        
        formGrid.setWidget(2, 0, new Label("Kamer"));
        formGrid.setWidget(2, 1, kamerField);
        
        formGrid.setWidget(3, 0, updateButton);
        formGrid.setWidget(3, 1, addButton);
        
    }

   //widgets neerzetten
    private void placeWidgets() {
        RootPanel.get(LISTING_ROOT_PANEL).add(listGrid);
        RootPanel.get(FORM_ROOT_PANEL).add(formGrid);
        RootPanel.get(TOOL_BAR_ROOT_PANEL).add(addNewButton);
    }

    //hide menutjes
    public void hideGui() {
    	formGrid.setVisible(false);
    	listGrid.setVisible(false);
    	addNewButton.setVisible(false);
    }

    
    // Load form.
  
    private void loadForm(Docent docent) {
        this.formGrid.setVisible(true);
        currentDocent = docent;
        currentPersoon = docent.getPersoon();
        this.naamField.setText(currentPersoon.getNaam());
        this.emailField.setText(docent.getEmail());
        this.kamerField.setText(docent.getKamer());
        
    }



    private void copyFieldDateTodocent() {
        currentPersoon.setNaam(naamField.getText());
        currentDocent.setEmail(emailField.getText());
        currentDocent.setPersoon(currentPersoon);
        currentDocent.setKamer(kamerField.getText());
    }

 
    public void gui_eventListGridClicked(Cell cellClicked) {
         int row = cellClicked.getRowIndex();
         int col = cellClicked.getCellIndex();
         Docent docent = this.Docenten.get(row);
         Persoon persoon = docent.getPersoon();
        
        
         if (col==EDIT_LINK) {
             this.addNewButton.setVisible(false);
             this.updateButton.setVisible(true);
             this.addButton.setVisible(false);
             loadForm(docent);
         } else if (col==REMOVE_LINK) {
        	 if (Window.confirm("Verwijderen docent ?")) {
        		 this.DocentService.removeDocent(docent);
        	 }
         }
    }


  
    public void gui_eventAddButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateTodocent();
        this.DocentService.addDocent(currentDocent);
    }


    public void gui_eventUpdateButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateTodocent();
        this.DocentService.updateDocent(currentDocent);
    }


    public void gui_eventAddNewButtonClicked() {
        this.addNewButton.setVisible(false);
        this.updateButton.setVisible(false);
        this.addButton.setVisible(true);
        Docent docent = new Docent();
        Persoon persoon = new Persoon();
        docent.setPersoon(persoon);
        loadForm(docent);
    }


    
    public void service_eventListRetrievedFromService(List<Docent> result) {

      
    	listGrid.setVisible(true);
    	addNewButton.setVisible(true);
        
        this.Docenten = result;
        this.listGrid.clear();
        this.listGrid.resizeRows(this.Docenten.size());
        int row = 0;
        this.addNewButton.setVisible(true);
        
        for (Docent docent : result) {
        	Persoon persoon = docent.getPersoon();
        	if (persoon == null) {
        		persoon = new Persoon("--");
        	}
        	this.listGrid.setWidget(row, 0, new Label (persoon.getNaam()));
            this.listGrid.setWidget(row, 1, new Label (docent.getEmail()));
            this.listGrid.setWidget(row, 2, new Label (docent.getKamer()));          
            this.listGrid.setWidget(row, EDIT_LINK, new Anchor("Edit"));
            this.listGrid.setWidget(row, REMOVE_LINK, new Anchor("Remove"));
            row ++;
        }
    }

    
    public void service_eventAddDocentSuccessful() {
        this.DocentService.listDocenten();
    }

  
    public void service_eventUpdateDocentSuccessful() {
        this.DocentService.listDocenten();
    }
    
   
    public void service_eventRemoveDocentSuccessful() {
        this.DocentService.listDocenten();
        
    }

 
    public void service_eventUpdateDocentFailed(Throwable caught) {
        Window.alert("Update docent failed");
    }

  
    public void service_eventAddDocentFailed(Throwable caught) {
    	Window.alert("Unable to update docent");
    }

 
    public void service_eventRemoveDocentFailed(Throwable caught) {
    	Window.alert("Remove docent failed");
    }

    public void service_eventListDocentenFailed(Throwable caught) {
    	Window.alert("Unable to get docent list");
        
    }

}
